﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace SN.Report
{
    public partial class FormLogin : Form
    {
        public FormLogin()
        {
            InitializeComponent();
        }
        public int LoginCount = 0;

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //
            string UserCode = txtUser.Text.Trim();
            string PassWord = txtPassword.Text.Trim();

            //PassWord = MD5Encrypt(PassWord);
            string Msg = string.Empty;

            SN.Services.UserService UserService = new Services.UserService();
            if (UserService.IsUserexist(UserCode, PassWord, ref Msg) != 1)
            {
                LoginCount += 1;
                if (LoginCount >= 4)
                {
                    MessageBox.Show("密码错误超过三次!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    DialogResult = DialogResult.Cancel;
                    this.Close();
                }
                else
                {
                    MessageBox.Show(Msg + string.Format("   第{0}次错误", LoginCount.ToString()), "提示!");
                    return;
                }
            }
            else
            {
                //记住用户名
                if (chkRemember.Checked)
                {
                    SN.Services.ConfigHelper.SetConfig("ChekUser", "true");
                    SN.Services.ConfigHelper.SetConfig("username", UserCode);
                }
                else
                {
                    SN.Services.ConfigHelper.SetConfig("ChekUser", "false");
                    SN.Services.ConfigHelper.SetConfig("username", UserCode);
                }
                //记住密码
                if (chkPassWord.Checked)
                {
                    SN.Services.ConfigHelper.SetConfig("ChekPassWord", "true");
                    SN.Services.ConfigHelper.SetConfig("PassWord", txtPassword.Text.Trim());
                }
                else
                {
                    SN.Services.ConfigHelper.SetConfig("ChekPassWord", "false");
                    SN.Services.ConfigHelper.SetConfig("PassWord", "");
                }

                DialogResult = DialogResult.OK;
                this.Close();
            }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FormLogin_Load(object sender, EventArgs e)
        {

            string xValue = SN.Services.ConfigHelper.GetConfig("ChekUser");
            string ChekPassWord = SN.Services.ConfigHelper.GetConfig("ChekPassWord");
            if (xValue == "true")
            {
                chkRemember.Checked = true;
                txtUser.Text = SN.Services.ConfigHelper.GetConfig("username");
            }
            if (ChekPassWord == "true")
            {
                chkPassWord.Checked = true;
                txtPassword.Text = SN.Services.ConfigHelper.GetConfig("PassWord");
            }

            btnConfirm.DialogResult = DialogResult.None;
            txtPassword.Focus();
        }

        private void chkPassWord_CheckedChanged(object sender, EventArgs e)
        {
            if (chkPassWord.Checked)
                chkRemember.Checked = true;
            else
                chkRemember.Checked = false;
        }

        /// <summary>
        /// MD5 加密静态方法
        /// </summary>
        /// <param name="EncryptString">待加密的密文</param>
        /// <returns>returns</returns>
        public static string MD5Encrypt(string EncryptString)
        {
            if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }

            MD5 m_ClassMD5 = new MD5CryptoServiceProvider();

            string m_strEncrypt = "";

            try
            {
                m_strEncrypt = BitConverter.ToString(m_ClassMD5.ComputeHash(Encoding.Default.GetBytes(EncryptString))).Replace("-", "");
            }
            catch (ArgumentException ex) { throw ex; }
            catch (CryptographicException ex) { throw ex; }
            catch (Exception ex) { throw ex; }
            finally { m_ClassMD5.Clear(); }

            return m_strEncrypt;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (FormConnect frm = new FormConnect())
            {
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    frm.Dispose();

                    this.Close();
                    Application.Exit(); //关闭程序
                    Application.Restart();
                    //Application.Restart();
                }

            }
        }


    }
}
